//和开启子进程相关

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>

#include "function.h"
#include "macro.h"
#include "configure.h"

static void StartWorkerProcesses(int thread_nums);
static int SpawnProcess(int thread_nums, const char *proc_name);
static void WorkerProcessCycle(int numbers, const char *proc_name);
static void WorkerProcessInit(int numbers);

static u_char master_process[] = "master process";

//主进程操作
void MasterProcessCycle() {
	sigset_t set;
	sigemptyset(&set);

	//在执行本函数过程中，需要屏蔽的信号
	sigaddset(&set, SIGCHLD);
	sigaddset(&set, SIGALRM);
	sigaddset(&set, SIGIO);
	sigaddset(&set, SIGINT);
	sigaddset(&set, SIGHUP);
	sigaddset(&set, SIGUSR1);
	sigaddset(&set, SIGUSR2);
	sigaddset(&set, SIGWINCH);
	sigaddset(&set, SIGTERM);
	sigaddset(&set, SIGQUIT);

	//设置屏蔽信号操作
	if(sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
		LogErrorCore(LOG_ALERT, errno, "MasterProcessCycle()中sigprocmask()失败!");
	}

	//设置主进程标题
	size_t size;
	int    i;
	size = sizeof(master_process);
	size += argv_need_mem;
	if(size < 1000) {
		char title[1000] = {0};
		strcpy(title, (const char *)master_process);
		strcat(title, " ");
		for(int i=0;i<os_argc;i++) {
			strcat(title, os_argv[i]);
		}
		SetProcTitle(title);
		LogErrorCore(LOG_NOTICE, 0, "%s %P 【master进程】启动并开始运行......!", title, current_pid);
	}

	//开始创建子进程
	Configure *configure = Configure::GetInstance();
	int worker_process = configure->GetIntDefault("WorkerProcesses",1);
	StartWorkerProcesses(worker_process);
	
	sigemptyset(&set);
	for(;;) {
		sigsuspend(&set);
		sleep(1);
	}
	return;
}

//根据给定的值创建子进程
static void StartWorkerProcesses(int thread_nums) {
	int i;
	for(i = 0; i<thread_nums; i++) {
		SpawnProcess(i, "worker process");
	}
	return;
}


//产生一个子进程
static int SpawnProcess(int numbers, const char *proc_name) {
	pid_t tmp_pid;
	tmp_pid = fork();
	switch(tmp_pid) {
	case -1://产生子进程失败
		LogErrorCore(LOG_ALERT, errno, "SpawnProcess()中fork()产生子进程num=%d,proc_name=\"%s\"失败!", numbers, proc_name);
		return -1;
	case 0://子进程分支
		parent_pid = current_pid;
		current_pid = getpid();
		WorkerProcessCycle(numbers, proc_name);
		break;
	default:
		break;
	}
	return tmp_pid;
}

//子进程功能函数
static void WorkerProcessCycle(int numbers, const char *proc_name) {
	process = PROCESS_WORKER;
	WorkerProcessInit(numbers);
	SetProcTitle(proc_name);
	LogErrorCore(LOG_NOTICE, 0, "%s %P 【worker进程】启动并开始运行......!", proc_name, current_pid);
	for(;;) {
		ProcessEventsAndTimers();//处理网络事件和定时器事	
	}
	thread_pool.StopAll();
	my_socket.ShutdownSubproc();
	return;
}

static void WorkerProcessInit(int numbers) {
	sigset_t set;
	sigemptyset(&set);
	if(sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
		LogErrorCore(LOG_ALERT, errno, "WorkerProcessInit()中sigprocmask()失败!");
	}
	
	//线程池相关代码
	Configure *configure = Configure::GetInstance();
	int tmp_thread_nums = configure->GetIntDefault("ProcMsgRecvWorkThreadCount", 5);//处理接收到的消息的线程池中线程数量
	if(thread_pool.Create(tmp_thread_nums) == false) {
		exit(-2);
	}
	sleep(1);
	if(my_socket.InitializeSubproc() == false) {
		exit(-2);
	}
	
	//网络相关代码
	my_socket.EpollInit();
	return;
}
